﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Rose.Helper;

namespace Rose.Controllers
{
    public class ImageController : Controller
    {
        //
        // GET: /Image/
        private Database_RoseEntities db = new Database_RoseEntities();

       

        public ImageResizeResult GetImage(string image, int width=100, int height=100, bool noZooming = false)
        {
            return new ImageResizeResult(image, width, height, noZooming);
        }
        //返回缩略图

        public ActionResult UploadHeadImagePart()
        {
            return View();
        }


         
        public ActionResult UploadHeadImage(HttpPostedFileBase  Filedata)

        {


            if (Filedata == null)
                return Content("错误");

            if (!Directory.Exists(Server.MapPath("/TempFile")))
                Directory.CreateDirectory(Server.MapPath("/TempFile"));

            string filepath = Server.MapPath("/TempFile") + "\\" + Path.GetFileName(Filedata.FileName);
            Filedata.SaveAs(filepath);
           
            return Content("/TempFile/"+Path.GetFileName(Filedata.FileName));
           
        }


        

        [HttpPost]
       
        public ActionResult CutHeadImage(int x,int y,int width,int height,string filename)
        {
            if(!Directory.Exists(Server.MapPath("/HeadImageFile")))
                Directory.CreateDirectory(Server.MapPath("/HeadImageFile"));

            ImageCut cut = new ImageCut();
            string[] splits=filename.Split('/');
            string[] extensions = filename.Split('.');

            string serverfilename = Server.MapPath("/TempFile") + "\\" + splits[splits.Length - 1];
            string guid= Guid.NewGuid().ToString();
            string saveFilePath = Server.MapPath("/HeadImageFile") + "\\" +guid+"."+extensions[extensions.Length-1];


            if (cut.CutImage(serverfilename, x, y, width, height, saveFilePath))
            {
                System.IO.File.Delete(serverfilename);
                return Content("/HeadImageFile/" +guid+"." +extensions[extensions.Length-1] );
            }
            //delete photo
            return Content("错误");
        }

        [HttpPost]
        
       public ActionResult UploadPhotos(HttpPostedFileBase Filedata,string albumId)
        {
            string userId=new CheckHelpModel().CheckCurrentUserId();

            if (Filedata == null)
                return Content("没有选择图片");

            if (string.IsNullOrEmpty(albumId))
            {
                Album album=db.Albums.Where(c => c.UserId == userId).FirstOrDefault();
                if (album == null)
                    return Content("没有上传的相册");
                albumId = (album).Id.ToString();
            }
            //这里的相册还没有决定好
            string[] extensions = Filedata.FileName.Split('.');
            string extension=extensions[extensions.Length-1];

            
            if (string.IsNullOrEmpty(userId))
            {
                userId = new CheckHelpModel().CheckAlbum(albumId).UserId;
            }
            //没有保留session
            string path="/Photos/"+userId+"/"+albumId;

            if (!Directory.Exists(Server.MapPath(path)))
                Directory.CreateDirectory(Server.MapPath(path));

            string filepath = Server.MapPath(path) + "\\" + new AlbumHelpModel().UploadPhoto(albumId,extension);
                //string filepath = Server.MapPath(path) + "\\" +Path.GetFileName(Filedata.FileName);
            Filedata.SaveAs(filepath);
          
           
            Image initImage = Image.FromFile(filepath,true);

            return Json(new {
                status="1",//判断此次上传是否成功，1为成功，其它为失败
                name=Filedata.FileName,//被上传的文件名
              width=initImage.Width,//图片宽度
              height=initImage.Height,//图片高度
              mime=extensions[extensions.Length-1]//图片mime类型
            }, JsonRequestBehavior.AllowGet);

         
        }
      

       


    }
}
